<template>
  <div>
    <div style="margin: 16px 0 0 30px ">
      <div style="width: 150px;float: left;">
        <el-input
          placeholder="请输入内容"
          v-model="input"
          clearable>
        </el-input>
      </div>
      <el-button type="primary" @click="search(input)">搜索</el-button>
      <el-button type="success" plain @click="refresh">刷新</el-button>
      <el-button  type="success" round @click="dialogFormVisiblePost = true" >添加</el-button>
    </div>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="二级ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.typeId }}
        </template>
      </el-table-column>
      <el-table-column label="小类名" width="120px">
        <template slot-scope="scope">
          {{ scope.row.typeName }}
        </template>
      </el-table-column>
      <el-table-column label="图片"   align="center">
        <template slot-scope="scope">
<!--          {{ scope.row.typeUrl }}-->
          <img :src="scope.row.typeUrl" width="150px">
        </template>
      </el-table-column>
      <el-table-column label="所属大类" width="110" align="center">
        <template slot-scope="scope">
          {{scope.row.category.categoryName }}
<!--          {{categoryName}}-->
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="center" width="200">
        <template slot-scope="scope">
          <!--          <el-button type="success" round @click="dialogFormVisiblePost = true">添加</el-button>-->
          <el-button type="danger" icon="el-icon-delete" circle @click="typesDel(scope.row.typeId)"> </el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="5"
      :current-page.sync="currentPage"
      background
      layout="prev, pager, next, jumper"
      :total="count">
    </el-pagination>
  </div>

    <!--添加商品弹窗-->
    <el-dialog title="添加小类"  :visible.sync="dialogFormVisiblePost">
      <el-form :model="listPost">
        <el-form-item>
          <h2>请输入需要添加的信息：</h2>
        </el-form-item>
        <el-form-item label="小类名字: " :label-width="formLabelWidth">
          <el-input v-model="listPost.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="对应的大类: " :label-width="formLabelWidth">
          <template>
            <el-select v-model="listPost.id" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.categoryId"
                :label="item.categoryName"
                :value="item.categoryId">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblePut = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisiblePostGo(listPost)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from '@/api/table'
import { typesList,pagingList,categoryList,dialogFormVisiblePostApi,searchApi,typesDelApi } from '@/api/server-types'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      currentPage:null, //页数
      input: null, //搜索数据
      options: [{
      }],
      listPost:[{
        id:null,
        name:null,
      }], //添加数据
      dialogFormVisiblePost: false, // 添加小类弹窗
      count: 18, //赋值意义，代码出差，分页不会有问题
      list: null,
      listLoading: true,
      formLabelWidth: '120px',
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 根据id删除
    typesDel(typesId) {
      var that = this;
      typesDelApi(typesId, function(msg) {
        alert(msg.data.msg);
          that.fetchData()

      })
    },


    //搜索
    //存在bug,想到方法是：搜索成功或，更改分页的页数为第一页 已解决
    search(input){
      var that = this;
      that.currentPage = 1;
      searchApi(input,function (msg) {
        that.list = msg.data;
        that.count = msg.count;  //分页条数
      })

    },

    /*根据id添加小类*/
    dialogFormVisiblePostGo(listPost){
      var that = this;
      dialogFormVisiblePostApi(listPost,function(msg){
        alert(msg.msg);
        if(msg.msg == '添加成功'){
          that.dialogFormVisiblePost = false;

          that.refresh();
        }
      })
    },

    //分页
    handleCurrentChange(val) {
      var that = this;
      pagingList(val,that.input,function(msg) {
        that.list = msg.data;
        if (msg.msg === '查询情况') {
          that.listLoading = false
        }
      })
    },

    //手动刷新
    refresh(){
      var that = this;
      that.input = null;//搜索数据 清空 防止数据污染
      that.currentPage = 1;
      that.fetchData();

    },


    fetchData() {
      var that = this;
      that.list　= null;
      that.listLoading = true;
      typesList(function(msg) {
        that.list = msg.data;
        that.count = msg.count;  //分页条数
        if (msg.msg === '查询成功！') {
          that.listLoading = false
        }
        console.log(that.list[0].category.categoryName);
      })

      categoryList(function (msg) {
        that.options = msg.data;
        console.log(that.options);

      })

      // this.listLoading = true
      // getList().then(response => {
      //   this.list = response.data.items
      //   this.listLoading = false
      // })
    }
  }
}
</script>
